<template>
  <div class="app-container">
    <el-row :gutter="20">
      <splitpanes :horizontal="this.$store.getters.device === 'mobile'" class="default-theme">
        <!--部门数据-->
        <pane size="16">
          <el-col>
            <div class="head-container">
              <el-input v-model="deptName" placeholder="请输入部门名称" clearable size="small"
                        prefix-icon="el-icon-search" style="margin-bottom: 20px"/>
            </div>
            <div class="head-container">
              <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false"
                       :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current
                       @node-click="handleNodeClick"/>
            </div>
          </el-col>
        </pane>
        <!--用户数据-->
        <pane size="84">
          <el-col>
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                     label-width="68px">
              <el-form-item label="用户名称" prop="userName">
                <el-input v-model="examQueryParams.nickName" placeholder="请输入用户名称" clearable style="width: 240px"
                          @keyup.enter.native="handleQuery"/>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryExam">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
            <el-table v-loading="loading" :data="examRecords" @current-change="handleSelectionChange">
              <el-table-column label="姓名" align="center" prop="nickName" :show-overflow-tooltip="true" width="120">
                <template slot-scope="scope">
                  <span>{{  scope.row.nickName }}</span>
                </template>
              </el-table-column>
              <el-table-column label="部门" align="center" prop="deptName" :show-overflow-tooltip="true" width="120">
                <template slot-scope="scope">
                  <span>{{  scope.row.deptName }}</span>
                </template>
              </el-table-column>
              <el-table-column label="考试日期" align="center" prop="createTime" :show-overflow-tooltip="true" width="180">
                <template slot-scope="scope">
                  <span>{{  scope.row.taskExamPaperAnswer.createTime }}</span>
                </template>
              </el-table-column>
              <el-table-column label="试卷名称" align="center" prop="paperName" :show-overflow-tooltip="true">
                <template slot-scope="scope">
          <span v-if="scope.row.taskExamPaperAnswer !== null">
           {{ scope.row.taskExamPaperAnswer.paperName }}
           </span>
                </template>
              </el-table-column>
              <!-- 考试分数、试卷分数、答对题目数量、总题目数量、耗时（秒）、答题试卷内容 -->
              <el-table-column label="考试分数" align="center" prop="taskExamPaperAnswer.userScore" :show-overflow-tooltip="true" >
                <template slot-scope="scope">
          <span v-if="scope.row.taskExamPaperAnswer !== null">
            {{ scope.row.taskExamPaperAnswer.userScore }}
          </span>
                </template>
              </el-table-column>
              <el-table-column label="试卷分数" align="center" prop="taskExamPaperAnswer.paperScore" :show-overflow-tooltip="true" >
                <template slot-scope="scope">
          <span v-if="scope.row.taskExamPaperAnswer !== null">
            {{ scope.row.taskExamPaperAnswer.paperScore }}
          </span>
                </template>
              </el-table-column>
              <el-table-column label="答对题目数量" align="center" prop="taskExamPaperAnswer.questionCorrect" :show-overflow-tooltip="true" >
                <template slot-scope="scope">
          <span v-if="scope.row.taskExamPaperAnswer !== null">
            {{ scope.row.taskExamPaperAnswer.questionCorrect }}
          </span>
                </template>
              </el-table-column>
              <el-table-column label="总题目数量" align="center" prop="taskExamPaperAnswer.questionCount" :show-overflow-tooltip="true" >
                <template slot-scope="scope">
          <span v-if="scope.row.taskExamPaperAnswer !== null">
            {{ scope.row.taskExamPaperAnswer.questionCount }}
          </span>
                </template>
              </el-table-column>
              <el-table-column label="耗时（秒）" align="center" prop="taskExamPaperAnswer.doTime" :show-overflow-tooltip="true">
                <template slot-scope="scope">
          <span v-if="scope.row.taskExamPaperAnswer !== null">
            {{ scope.row.taskExamPaperAnswer.doTime }}
          </span>
                </template>
              </el-table-column>
            </el-table>

            <!-- 考试记录分页 -->
            <pagination
              v-show="totalExamRecords > 0"
              :total="totalExamRecords"
              :page.sync="examQueryParams.pageNum"
              :limit.sync="examQueryParams.pageSize"
              @pagination="getExamRecords"
            />
          </el-col>
        </pane>
      </splitpanes>
    </el-row>
  </div>
</template>

<script>
import {listUser, deptTreeSelect} from "@/api/system/user";
import {getToken} from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {Splitpanes, Pane} from "splitpanes";
import "splitpanes/dist/splitpanes.css";
import {detailList} from "@/api/exam/task";
export default {
  name: "User_Detail",
  dicts: ['sys_normal_disable', 'sys_user_sex', 'technical_title', 'technical_level', 'education'],
  components: {Treeselect, Splitpanes, Pane},
  data() {

    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      currentRow: null,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: "",
      // 所有部门树选项
      deptOptions: undefined,
      // 过滤掉已禁用部门树选项
      enabledDeptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 默认密码
      initPassword: undefined,
      // 日期范围
      dateRange: [],
      // 岗位选项
      postOptions: [],
      // 角色选项
      roleOptions: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 总条数
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: -1,
      },
      examQueryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/user/importData"
      },
      examRecords: [], // 存储考试记录数据
      totalExamRecords: 0, // 存储考试记录总数

    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getDeptTree();
    this.getConfigKey("sys.user.initPassword").then(response => {
      this.initPassword = response.msg;
    });
    // this.getExamRecords();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
        //打印树结构的根节点
        console.log('部门树结构', this.deptOptions);
        //获取第一个根部的depId作为默认选中部门
         this.examQueryParams.deptId = this.deptOptions[0].id;
        this.getExamRecords();
        // 过滤掉已禁用部门树选项
        this.enabledDeptOptions = this.filterDisabledDept(JSON.parse(JSON.stringify(response.data)));
      });
    },
    // 过滤禁用的部门
    filterDisabledDept(deptList) {
      return deptList.filter(dept => {
        if (dept.disabled) {
          return false;
        }
        if (dept.children && dept.children.length) {
          dept.children = this.filterDisabledDept(dept.children);
        }
        return true;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      // this.queryParams.deptId = data.id;
      this.examQueryParams.deptId = data.id;
      this.handleQueryExam();
    },
    getExamRecords() {
      detailList(this.examQueryParams).then(response => {
        console.log('考试记录列表', response);
        //过滤taskStatus为1的记录
        // this.examRecords = response.rows.filter(item => item.taskStatus === 1);
        // console.log('考试记录列表过滤后', this.examRecords);
        this.examRecords = response.rows
        this.totalExamRecords = response.total; // 更新总条数
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.currentRow = null;

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleQueryExam() {
      this.examQueryParams.pageNum = 1;
      this.getExamRecords();
    },
    // 日期范围
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.deptId = undefined;
      this.$refs.tree.setCurrentKey(null);
      this.handleQuery();
    },
    // 选中数据
    handleSelectionChange(selection) {
      this.currentRow = selection;
    },


  }
};
</script>
